<template>
    <div class="col-data-list">
        <draggable tag="div" class="option-list" :list="colData" v-bind="data.dragOption">
            <template #item="{ element,index }">
                <div>
                    <div class="col-data-item" :class="{active: element.active}" @click="selectNode(element)">
                        <div class="col-data-item-icon" @click="toggleOpenNode(element)">
                            <CaretRightOutlined v-if="!element.isLeaf" class="item-icon"
                                :class="{isOpen: element.open == true}" />
                        </div>
                        <div class="col-data-item-title" :class="element.isLeaf ? 'col-drag-handle' : ''">
                            {{ element.name }}</div>
                    </div>
                    <div class="child-node-list" v-show="element.open">
                        <colTreeNode :colData="element.children"></colTreeNode>
                    </div>
                </div>
            </template>
        </draggable>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import draggable from "vuedraggable";

defineOptions({
    name: 'colTreeNode'
})

const props = defineProps({
    colData: {
        type: Array,
        default: () => []
    }
})

const data = reactive({
    dragOption: {
        group: { name: 'col-draggable', pull: 'clone', put: false },
        sort: false,
        animation: 180,
        handle: '.col-drag-handle'
    }
})

const toggleOpenNode = (item) => {
    item.open = !item.open;
}

const selectNode = (item) => {
    clearActive(() => {
        item.active = true;
    });
}


</script>

<style lang="less" scoped>
.col-drag-handle {
    cursor: pointer;
}

.col-data-item {
    display: flex;
    margin-bottom: 2px;
    align-items: center;
    
    ::selection {
        background-color: transparent;
    }

    .col-data-item-icon {
        margin-right: 5px;
        font-size: 12px;
        cursor: pointer;

        .item-icon {
            transition: all 0.1s;
        }

        .isOpen {
            transform: rotate(90deg);
        }
    }

    .col-data-item-title {
        font-size: 14px;
        padding: 4px;
        cursor: pointer;
        border-radius: 5px;
        transition: all .1s;
    }

    &:hover {
        .col-data-item-title {
            background-color: #e6f4ff;
        }
    }
}

.child-node-list {
    padding-left: 15px;
}
</style>